package com.yinhe.music.yhmusic.base;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.ViewGroup;

import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.log.MLog;
import com.yinhe.music.yhmusic.music.PlayQueueFragment;
import com.yinhe.music.yhmusic.service.PlayService;
import com.yinhe.music.yhmusic.utils.SafeDispatchHandler;


/**
 * @author wm
 * @date 2016/3/17
 */
public class BaseFragment extends Fragment implements IServiceConnect {
    public final String TAG = getClass().getSimpleName();

    PlayQueueFragment playQueueFragment = new PlayQueueFragment();

    protected PlayService mService;
    protected SafeDispatchHandler mHandler = new SafeDispatchHandler(Looper.getMainLooper());

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MLog.info(this, "fragment(%s) onCreate", getClass().getSimpleName());
    }

    @Override
    public void onResume() {
        super.onResume();
        MLog.info(this, "fragment(%s) onResume", getClass().getSimpleName());
    }

    @Override
    public void onPause() {
        super.onPause();
        MLog.info(this, "fragment(%s) onPause", getClass().getSimpleName());
    }

    @Override
    public void onStop() {
        super.onStop();
        MLog.info(this, "fragment(%s) onStop", getClass().getSimpleName());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
        }
        MLog.info(this, "fragment(%s) onDestroy", getClass().getSimpleName());
    }

    /**
     * 显示播放列表
     */
    public void showPlayingList() {
        playQueueFragment.show(getFragmentManager(), "playlistframent");
    }

    public View getEmptyView(View parent){
        return getLayoutInflater().inflate(R.layout.include_empty_layout, (ViewGroup) parent.getParent(), false);
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        bindService();
    }

    @Override
    public void onDetach() {
        unBindService();
        super.onDetach();
    }

    private ServiceConnection servConn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mService = ((PlayService.PlayBinder) service).getService();
            BaseFragment.this.onServiceConnected(mService);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mService = null;
            BaseFragment.this.onServiceDisconnected();
        }
    };

    @Override
    public void bindService() {
        if (servConn == null) {
            return;
        }

        Intent intent = new Intent();
        intent.setClass(getActivity(), PlayService.class);
        getActivity().bindService(intent, servConn, Context.BIND_AUTO_CREATE);
    }


    @Override
    public void unBindService() {
        if (servConn != null) {
            getActivity().unbindService(servConn);
            servConn = null;
        }
    }

    /**
     * 建立与Service连接回调
     *
     * @param service 绑定的服务
     */
    public void onServiceConnected(PlayService service) {
        MLog.info(TAG, "onServiceConnected");
    }

    /**
     * 与服务丢失连接时回调
     */
    public void onServiceDisconnected() {
        MLog.info(TAG, "onServiceDisconnected");
    }

}
